package rpcremote

import (
	"google.golang.org/grpc"
	"os"
	appPD "git.oschina.net/YPTArch/appcenter/rpc/pd"
	authPD "git.oschina.net/YPTArch/securityauthcenter/rpc/pd"
	"git.oschina.net/YPTArch/usercenter/config"
	"git.oschina.net/YPTArch/basic/sys/logs"
)
const (
	appConnAddress     = "192.168.1.100:10001"
	authConnAddress     = "192.168.1.100:10010"
)

var appConn *grpc.ClientConn
var authConn *grpc.ClientConn

func GetApplicationClient() appPD.ApplicationClient  {
	return appPD.NewApplicationClient(appConn)
}
func GetUserAuthAppClient() authPD.UserAuthAppClient  {
	return authPD.NewUserAuthAppClient(authConn)
}
func init()  {
	appConn=RunClient(config.GetAppConnAddress())
	authConn=RunClient(config.GetAuthConnAddress())
}

func RunClient(addr string)(*grpc.ClientConn) {
	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	//defer conn.Close()
	if err != nil {
		logs.Logger.Error(addr+" ----->did not connect: "+err.Error())
		os.Exit(2)
	}
	logs.Logger.Info(addr+" ----> client connect success!")
	return conn
}